package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.service.AdminConfigService;
import com.cskaoyan.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

public class AdminConfigServiceImpl implements AdminConfigService {

    static  List<String> nameList;

    static {
        nameList=new ArrayList<>();
        nameList.add("market_mall_longitude");
        nameList.add("market_mall_latitude");
        nameList.add("market_mall_address");
        nameList.add("market_mall_qq");
        nameList.add("market_mall_phone");
        nameList.add("market_mall_name");
    }

    @Override
    public Map<String,Object> getExpressResponse() {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        List<MarketSystem> dataBaseMarketSystem = mapper.selectByExample(new MarketSystemExample());
        Map<String, Object> map = marketSystems2Map(dataBaseMarketSystem);
        HashMap<String, Object> reMap = new HashMap<>();

        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (key.equals("market_express_freight_min")) {
                reMap.put("market_express_freight_min", map.get(key));
            } else if (key.equals("market_express_freight_value")) {
                reMap.put("market_express_freight_value", map.get(key));
            }
        }
        session.commit();
        session.close();
        return reMap;
    }

    @Override
    public Map<String,Object> getWxResponse() {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        List<MarketSystem> dataBaseMarketSystem = mapper.selectByExample(new MarketSystemExample());
        Map<String, Object> map = marketSystems2Map(dataBaseMarketSystem);
        HashMap<String, Object> reMap = new HashMap<>();

        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (key.equals("market_wx_index_new")) {
                reMap.put("market_wx_index_new", map.get(key));
            } else if (key.equals("market_wx_index_topic")) {
                reMap.put("market_wx_index_topic", map.get(key));
            }else if (key.equals("market_wx_share")) {
                reMap.put("market_wx_share", map.get(key));
            }else if (key.equals("market_wx_index_brand")) {
                reMap.put("market_wx_index_brand", map.get(key));
            }else if (key.equals("market_wx_catlog_goods")) {
                reMap.put("market_wx_catlog_goods", map.get(key));
            }else if (key.equals("market_wx_catlog_list")) {
                reMap.put("market_wx_catlog_list", map.get(key));
            }else if (key.equals("market_wx_index_hot")) {
                reMap.put("market_wx_index_hot", map.get(key));
            }
        }
        session.commit();
        session.close();
        return reMap;
    }

    @Override
    public Map<String,Object> getOrderResponse() {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        List<MarketSystem> dataBaseMarketSystem = mapper.selectByExample(new MarketSystemExample());
        Map<String, Object> map = marketSystems2Map(dataBaseMarketSystem);
        HashMap<String, Object> reMap = new HashMap<>();

        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (key.equals("market_order_comment")) {
                reMap.put("market_order_comment", map.get(key));
            } else if (key.equals("market_order_unconfirm")) {
                reMap.put("market_order_unconfirm", map.get(key));
            }else if (key.equals("market_order_unpaid")) {
                reMap.put("market_order_unpaid", map.get(key));
            }
        }
        session.commit();
        session.close();
        return reMap;
    }

    @Override
    public void modifyOrder(String comment, String unconfirm, String unpaid) {
        Map<String, String> expressMap = new HashMap<>();
        expressMap.put("market_order_comment",comment);
        expressMap.put("market_order_unconfirm",unconfirm);
        expressMap.put("market_order_unpaid",unpaid);
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        Set<String> set = expressMap.keySet();
        for (String k : set) {
            MarketSystemExample example = new MarketSystemExample();
            MarketSystem marketSystem = new MarketSystem();
            example.or().andKeyNameEqualTo(k);
            marketSystem.setKeyValue(expressMap.get(k));
            mapper.updateByExampleSelective(marketSystem,example);
        }
        session.commit();
        session.close();
    }

    @Override
    public void modifyExpress(String min, String value) {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        int i = mapper.updateByName(min,"market_express_freight_min");
        int i1 = mapper.updateByName(value,"market_express_freight_value");
        session.commit();
        session.close();
    }

    @Override
    public void modifyWx(String goods, String list, String brand, String hot, String catlogNew, String topic, String catlogFalse) {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        int i = mapper.updateByName(goods,"market_wx_catlog_goods");
        int i1 = mapper.updateByName(list,"market_wx_catlog_list");
        int i2 = mapper.updateByName(brand,"market_wx_index_brand");
        int i3 = mapper.updateByName(hot,"market_wx_index_hot");
        int i4 = mapper.updateByName(catlogNew,"market_wx_index_new");
        int i5 = mapper.updateByName(topic,"market_wx_index_topic");
        int i6 = mapper.updateByName(catlogFalse,"market_wx_share");
        session.commit();
        session.close();
    }

    @Override
    public void postMall(String marketMallAddress, String marketMallLatitude, String marketMallLongitude, String marketMallName, String marketMallPhone, String marketMallQq) {
        SqlSession session= MybatisUtil.getSession();
        MarketSystemMapper configMapper = session.getMapper(MarketSystemMapper.class);
        Iterator<String> stringIterator = nameList.iterator();
        while (stringIterator.hasNext()){
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
            String keyName = stringIterator.next();
            criteria.andKeyNameEqualTo(keyName);
            List<MarketSystem> marketSystems = configMapper.selectByExample(marketSystemExample);
            switch (keyName){
                case "market_mall_longitude":
                    marketSystems.get(0).setKeyValue(marketMallLongitude);
                    break;
                case "market_mall_latitude":
                    marketSystems.get(0).setKeyValue(marketMallLatitude);
                    break;
                case "market_mall_address":
                    marketSystems.get(0).setKeyValue(marketMallAddress);
                    break;
                case "market_mall_qq":
                    marketSystems.get(0).setKeyValue(marketMallQq);
                    break;
                case "market_mall_phone":
                    marketSystems.get(0).setKeyValue(marketMallPhone);
                    break;
                case "market_mall_name":
                    marketSystems.get(0).setKeyValue(marketMallName);
                    break;
            }
            configMapper.updateByExample(marketSystems.get(0),marketSystemExample);
        }
        session.commit();
        session.close();
    }

    @Override
    public Map<String, String> mall() {
        SqlSession session= MybatisUtil.getSession();
        MarketSystemMapper configMapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameIn(nameList);
        List<MarketSystem> marketSystems = configMapper.selectByExample(marketSystemExample);
        Map<String,String> mallMap=new HashMap<>();
        Iterator<MarketSystem> iterator = marketSystems.iterator();
        while (iterator.hasNext()){
            MarketSystem ms=iterator.next();
            mallMap.put(ms.getKeyName(),ms.getKeyValue());
        }
        session.commit();
        session.close();
        return mallMap;
    }

    private Map<String, Object> marketSystems2Map(List<MarketSystem> dataBaseMarketSystem) {
        Map<String, Object> map = new HashMap<>();
        for (MarketSystem system : dataBaseMarketSystem) {
            map.put(system.getKeyName(), system.getKeyValue());
        }
        return map;
    }
}
